﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class GetTheMostFarPathLengthCommand : HYExternalCommand
	{
		public override void FilterProcess()
		{
            base.FilterViewType.Add((ViewType)4);
			base.FilterProcess();
		}

		public override Result HYExecute(ref string TranName)
		{
			TranName = "检测水管最远路径";
			SearchBranch searchBranch = new SearchBranch(YArchitech.Revit.ElementType.Pipe, YArchitech.Revit.ElementType.Sprinklers, base.UiDoc);
			searchBranch.PickFirstElement(new PipeFilter(), "请选择一根主水管……");
			searchBranch.DoSearch();
			ElementNode listRoot = searchBranch.GetListRoot();
			List<PipeInfo> list = new List<PipeInfo>();
			List<List<PipeInfo>> list2 = new List<List<PipeInfo>>();
			int num = 0;
			int num2 = 0;
			double num3 = 0.0;
			double num4 = 0.0;
			this.AddElementNode(listRoot, ref list, ref list2, ref num, ref num2);
			List<double> list3 = new List<double>();
			foreach (List<PipeInfo> list4 in list2)
			{
				foreach (PipeInfo pipeInfo in list4)
				{
					if (pipeInfo.ENode.Type == YArchitech.Revit.ElementType.Pipe)
					{
						Pipe pipe = (Pipe)pipeInfo.ENode.Element;
						num3 += (pipe.Location as LocationCurve).Curve.Length;
					}
				}
				list3.Add(num3);
				num3 = 0.0;
			}
			foreach (double num5 in list3)
			{
				num4 = ((num4 > num5) ? num4 : num5);
			}
			int index = list3.IndexOf(num4);
			List<Element> list5 = new List<Element>();
			foreach (PipeInfo pipeInfo2 in list2.ElementAt(index))
			{
				list5.Add(pipeInfo2.ENode.Element);
			}
			RevitVersionFuncs.SetSelection(base.Sel, list5);
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public void AddElementNode(ElementNode eNode, ref List<PipeInfo> listInfo, ref List<List<PipeInfo>> listPath, ref int id, ref int count)
		{
			if (eNode == null)
			{
				count++;
				List<PipeInfo> list = new List<PipeInfo>();
				foreach (PipeInfo item in listInfo)
				{
					list.Add(item);
				}
				listPath.Add(list);
				return;
			}
			PipeInfo pipeInfo = new PipeInfo();
			if (id == 0)
			{
				pipeInfo.ParentId = "";
			}
			else
			{
				pipeInfo.ParentId = eNode.Preview.Element.Id.ToString();
			}
			id++;
			pipeInfo.ElemId = eNode.Element.Id.ToString();
			pipeInfo.ENode = eNode;
			listInfo.Add(pipeInfo);
			this.AddElementNode(eNode.Next, ref listInfo, ref listPath, ref id, ref count);
			if (eNode.SubElementNode != null)
			{
				foreach (ElementNode eNode2 in eNode.SubElementNode)
				{
					this.AddElementNode(eNode2, ref listInfo, ref listPath, ref id, ref count);
				}
			}
			foreach (PipeInfo pipeInfo2 in listInfo)
			{
				if (pipeInfo2.ElemId == eNode.Element.Id.ToString())
				{
					listInfo.Remove(pipeInfo2);
					break;
				}
			}
		}
	}
}
